﻿#ifndef CUSTOMCHECKBOX_H
#define CUSTOMCHECKBOX_H
#include <QPushButton>
#include <QFrame>
#include <QHBoxLayout>
#include <QVector>
#include <QString>
#include "styleDefine.h"

// 单个按钮
class MyCheckBox : public QPushButton
{
    Q_OBJECT
public:
    MyCheckBox(uint nIndex, QString strSelected, QString strNoSelected, QWidget* parent = nullptr)
        : QPushButton(parent), m_bLockStatus(false), m_nBoxIndex(nIndex), m_bSelected(false), m_strSelected(strSelected), m_strNoSelected(strNoSelected) {
        setStyleSheet("QPushButton { border: none; background-color: transparent; }");
        changeStatus();
        connect(this, SIGNAL(clicked()), this, SLOT(slt_click()));
    }

    virtual ~MyCheckBox() {}
    void setLock() {
        m_bLockStatus = true;
    }
    void setStatus(bool bStatus)
    {
        m_bSelected = bStatus;
        changeStatus();
    }

    bool getStatus(){
        return m_bSelected;
    }

    uint getIndex(){
        return m_nBoxIndex;
    }

private slots:
    // 点击处理
    void slt_click() {
        if (true == m_bLockStatus)
        {
            if (false == m_bSelected)
            {
                m_bSelected = true;
            }
        }
        else
        {
            m_bSelected = !m_bSelected;
        }

        changeStatus();
        emit SendClicked(m_nBoxIndex);
    }

signals:
    // 发送点击信号到上层
    void SendClicked(uint);

private:
    // 更新状态对应的图标展示
    void changeStatus(){
        if(false == m_bSelected)
        {
            setIcon(QIcon(":/common/resource/common/check_wait.ico"));
            setText(m_strNoSelected);
        }
        else
        {
            setIcon(QIcon(":/common/resource/common/check_yes.ico"));
            setText(m_strSelected);
        }
    }

private:
    bool    m_bLockStatus;      // 按钮重复点击是否变更
    uint    m_nBoxIndex;        // 按钮指定序号
    bool    m_bSelected;        // 被选中状态
    QString m_strSelected;      // 选中文本
    QString m_strNoSelected;    // 未选中文本
};

class CustomCheckBoxList : public QFrame
{
    Q_OBJECT
public:
    CustomCheckBoxList(QWidget* parent = nullptr) : QFrame(parent), m_nCurrentIndex(INVALID_VALUE), m_pLayout(nullptr) {
        m_vecCheckBox.clear();
        m_pLayout = new QHBoxLayout;
        m_pLayout->setSpacing(0);
        m_pLayout->setContentsMargins(0, 0, 0, 0);
        this->setLayout(m_pLayout);
    }

    void addCheckOne(const QString &strSelected, const QString &strNoSelected){
        MyCheckBox* pCheckOne = new MyCheckBox(m_vecCheckBox.size(), strSelected, strNoSelected, this);
        connect(pCheckOne, SIGNAL(SendClicked(uint)), this, SLOT(slt_clicked(uint)));
        pCheckOne->setLock();
        m_pLayout->addWidget(pCheckOne);
        m_vecCheckBox.push_back(pCheckOne);
    }

    void setDefault(const uint nIndex){
        if (nIndex < (uint)m_vecCheckBox.size())
        {
            m_vecCheckBox[nIndex]->setStatus(true);
            slt_clicked(nIndex);
        }
    }

    uint getSelectIndex(){
        return m_nCurrentIndex;
    }

    ~CustomCheckBoxList() {}
private slots:
    // 点击信号处理
    void slt_clicked(uint nIndex) {
        m_nCurrentIndex = nIndex;
        for (auto& it : m_vecCheckBox)
        {
            if (nIndex != it->getIndex())
            {
                it->setStatus(false);
            }
        }

        emit SendClickedIndex(m_nCurrentIndex);
    }
signals:
    // 发送点击信号到上层
    void SendClickedIndex(uint);

private:
    uint                 m_nCurrentIndex;   // 被选中序号
    QHBoxLayout*         m_pLayout;         // 布局
    QVector<MyCheckBox*> m_vecCheckBox;     // 按钮列表
};

#endif // CUSTOMCHECKBOX_H